package tripx.system;

import tripx.db.DbServiceInvoker;
import tripx.server.TripxServer;

/**
 * The class handles the most of the configuration, and master the common object
 * which exists in the interaction between modules.
 * 
 * @author zhb
 * 
 */
public class TripxSystem {

	/**
	 * the single instance of this object
	 */
	private static TripxSystem sys;

	private SysConfig config;

	private TripxServer server;

	private DbServiceInvoker invoker;

	/* private constructor */
	private TripxSystem() {
		config = new SysConfig();
		setup();
	}

	public static TripxSystem getInstance() {
		if (sys == null) {
			sys = new TripxSystem();
		}
		return sys;
	}

	/**
	 * start up the service server.
	 */
	public void startup() {
		/* start the service server */
		server.start();
	}

	/**
	 * shutdown the service server, although it may never be called
	 */
	public void shutdown() {
		/* shutdown the service server */
		server.shutdown();
	}

	/**
	 * get the database service invoker
	 * 
	 * @return
	 */
	public DbServiceInvoker getDbServiceInvoker() {
		return invoker;
	}

	/**
	 * get the Global Configuration Object
	 * 
	 * @return
	 */
	public SysConfig getSystemConfig() {
		return config;
	}

	// -------------------------------------------------------------
	// --------- setup the server and the dbserviceinvoker ---------
	// -------------------------------------------------------------
	private void setup() {
		// initialize the server
		server = new TripxServer(config.getPropertyHandlerMapping(),
				config.getServerPort());
		// initialize the invoker
		invoker = new DbServiceInvoker(config.getDbServerAddr());
	}
}
